﻿using Bangle.BLL;
using Bangle.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Phone_Classification : BasePage
{
    #region 属性

    private string StrWhere
    {
        get
        {
            return ViewState["StrWhere"] == null ? " 1=1 " : ViewState["StrWhere"].ToString();
        }
        set
        {
            ViewState["StrWhere"] = value;
        }
    }

    /// <summary>
    /// ParentId
    /// </summary>
    private int ParentId
    {
        get
        {
            return Convert.ToInt32(ViewState["ParentId"]);
        }
        set
        {
            ViewState["ParentId"] = value;
        }
    }

    private string Code
    {
        get
        {
            return base.ParseQueryString("Code", "");
        }
    }

    private string pageCode
    {
        get
        {
            string str = "";
            switch (Code)
            {
                case "Screen": str = "002004"; break;
                case "Chip": str = "002005"; break;
                case "Series": str = "002006"; break;
                case "RecordType": str = "003002"; break;
                case "ProgressType": str = "003003"; break;
                case "SupplierType": str = "004001"; break;
                case "CustomerType": str = "003004"; break;
                case "SaleState": str = "005101"; break;
            }
            return str;
        }
    }
    #endregion

    T_ClassificationBLL bll = new T_ClassificationBLL();

    #region 加载
    protected void Page_Load(object sender, EventArgs e)
    {
        setUserControlStatus();

        if (!IsPostBack)
        {
            T_Classification model = bll.GetModelList(string.Format("ClassificationCode='{0}'", Code)).FirstOrDefault();
            ParentId = model.ClassificationId;
            this.lbl_Title.Text = model.ClassificationName;
            this.btn_Add.PostBackUrl = "ClassificationEdit.aspx?Action=Add&Code=" + Code;

            //设置分页控件文本
            setLanguage();

            //绑定数据
            BindRepeaterData("1=1");
        }
    }

    /// <summary>
    /// 设置分页控件文本
    /// </summary>
    private void setLanguage()
    {
        TextUtility.SetPaginateCtlText(PaginateCtl);
    }
    #endregion

    #region 检验用户权限
    private void setUserControlStatus()
    {
        //检验用户权限
        base.CheckUserPermission(pageCode);
        this.btn_Add.Enabled = base.CheckUserPermissionOperation(pageCode, "Add");
    }
    #endregion

    #region 数据绑定
    private void BindRepeaterData(string swhere)
    {
        PaginateCtl.PageSize = base.CurrentSession.PageSize;
        int pageIndex = PaginateCtl.CurrentPageIndex;
        int pageSize = PaginateCtl.PageSize;

        UtilityBLL ubll = new UtilityBLL();

        int rCount = 0;
        swhere += string.Format(" and ParentId={0}", ParentId);
        DataTable dt = ubll.GetListByPage("T_Classification", "*", "ClassificationId", swhere, pageSize, pageIndex, 0, out rCount);

        //绑定数据源
        RepeaterList.DataSource = dt;
        //返回记录条数
        PaginateCtl.RecordCount = rCount;

        //设置记录总数的显示文本(此方法必须在设置PaginateCtl.RecordCount之后调用)
        TextUtility.SetPaginateCtlRecoText(PaginateCtl);
        RepeaterList.DataBind();
    }
    #endregion

    #region 查询
    /// <summary>
    /// 查询按钮
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btn_Search_Click(object sender, EventArgs e)
    {
        string swhere = "1=1";
        if (this.txt_ClassificationName.Text.Trim().Length > 0)
        {
            swhere += " and ClassificationName like '%" + TextUtility.ReplaceText(this.txt_ClassificationName.Text.Trim()) + "%'";
        }
        StrWhere = swhere;
        //分页控件回到第1页
        PaginateCtl.CurrentPageIndex = 1;
        BindRepeaterData(StrWhere);
    }
    #endregion

    #region 列表操作
    protected void RepeaterList_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            #region 用户权限
            LinkButton lbtn_Detail = (LinkButton)e.Item.FindControl("lbtn_Detail");
            LinkButton lbtn_Modify = (LinkButton)e.Item.FindControl("lbtn_Modify");
            LinkButton lbtn_Delete = (LinkButton)e.Item.FindControl("lbtn_Delete");

            lbtn_Detail.Visible = base.CheckUserPermissionOperation(pageCode, "View");
            lbtn_Modify.Visible = base.CheckUserPermissionOperation(pageCode, "Edit");
            lbtn_Delete.Visible = base.CheckUserPermissionOperation(pageCode, "Del");
            #endregion
        }
    }
    protected void RepeaterList_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        int id = Convert.ToInt32(e.CommandArgument);
        if (e.CommandName == "Delete")
        {
            try
            {
                bll.Delete(id);
                BindRepeaterData(StrWhere);
            }
            catch (Exception ex)
            {
                base.AlertError(ex.ToString());
            }

        }
        else if (e.CommandName == "Modify")
        {
            Response.Redirect("ClassificationEdit.aspx?Action=Modify&id=" + id + "&Code=" + Code);
        }
        else if (e.CommandName == "Detail")
        {
            Response.Redirect("ClassificationEdit.aspx?Action=Detail&id=" + id + "&Code=" + Code);
        }
    }
    #endregion

    #region 分页
    /// <summary>
    /// 分页事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void PaginateCtl_PageChanged(object sender, EventArgs e)
    {
        BindRepeaterData(StrWhere);
    }
    #endregion

}